package com.jwdf.uda.ui.fragment.user.register;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentSecureAccountBinding;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.RegisterStep3Action;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.user.bean.UserSession;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/24.
 */
public class SecureAccountFragment extends BaseFragment {

    private FragmentSecureAccountBinding mDataBinding;
    private String mMobile;
    private String mAuthCode;
    public View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UdaUtils.hideInputMethod(getContext(), mDataBinding.inputEditUserName);
            if (R.id.btn_comlete_info == v.getId()) {
                mDataBinding.ivClearUserNameInput.setVisibility(View.GONE);
                mDataBinding.ivClearPasswdInput.setVisibility(View.GONE);
                mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.GONE);
                String usrName = mDataBinding.inputEditUserName.getText().toString().trim();
                String passwd = mDataBinding.inputEditPasswd.getText().toString().trim();
                String confirmPasswd = mDataBinding.inputEditConfirmPasswd.getText().toString().trim();
                if (TextUtils.isEmpty(usrName)) {
                    ToastUtils.show(getContext(), getString(R.string.user_can_not_be_null));
                    return;
                }
                if (TextUtils.isEmpty(passwd)) {
                    ToastUtils.show(getContext(), getString(R.string.pwd_can_not_be_null));
                    return;
                }
                if (TextUtils.isEmpty(confirmPasswd)) {
                    ToastUtils.show(getContext(), getString(R.string.confirm_pwd_can_not_be_null));
                    return;
                }

                if (!TextUtils.equals(passwd, confirmPasswd)) {
                    ToastUtils.show(getContext(), getString(R.string.twice_pwd_not_equal));
                    return;
                }
                completeAccount(usrName, passwd);
            } else if (R.id.iv_clear_user_name_input == v.getId()) {
                mDataBinding.inputEditUserName.setText("");
            } else if (R.id.iv_clear_passwd_input == v.getId()) {
                mDataBinding.inputEditPasswd.setText("");
            } else if (R.id.iv_clear_confirm_passwd_input == v.getId()) {
                mDataBinding.inputEditConfirmPasswd.setText("");
            }
        }
    };
    private View.OnFocusChangeListener mInputFocusListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (R.id.input_edit_user_name == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearUserNameInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditUserName.getText().toString().length() > 0) {
                        mDataBinding.ivClearUserNameInput.setVisibility(View.VISIBLE);
                    }
                }
            } else if (R.id.input_edit_passwd == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearPasswdInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditPasswd.getText().toString().length() > 0) {
                        mDataBinding.ivClearPasswdInput.setVisibility(View.VISIBLE);
                    }
                }
            } else if (R.id.input_edit_confirm_passwd == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditConfirmPasswd.getText().toString().length() > 0) {
                        mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    };
    private TextWatcher mUserTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditUserName.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditUserName.isFocused()) {
                mDataBinding.ivClearUserNameInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
        }
    };
    private TextWatcher mPassTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditPasswd.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditPasswd.isFocused()) {
                mDataBinding.ivClearPasswdInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
        }
    };
    private TextWatcher mConfirmPassTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditConfirmPasswd.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditConfirmPasswd.isFocused()) {
                mDataBinding.ivClearConfirmPasswdInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentSecureAccountBinding.inflate(inflater, container, false);
        mDataBinding.setSecureAccountFrament(this);
        initViews();
        return mDataBinding.getRoot();
    }
    private void initViews() {
        mMobile = getArguments().getString("mobile", "");
        mAuthCode = getArguments().getString("auth_code", "");
        mDataBinding.inputEditUserName.addTextChangedListener(mUserTextChangeListener);
        mDataBinding.inputEditPasswd.addTextChangedListener(mPassTextChangeListener);
        mDataBinding.inputEditConfirmPasswd.addTextChangedListener(mConfirmPassTextChangeListener);
        mDataBinding.inputEditUserName.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.inputEditPasswd.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.inputEditConfirmPasswd.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.ivClearUserNameInput.setOnClickListener(mClickListener);
        mDataBinding.ivClearPasswdInput.setOnClickListener(mClickListener);
        mDataBinding.ivClearConfirmPasswdInput.setOnClickListener(mClickListener);
        mDataBinding.inputEditUserName.setFilters(new InputFilter[]{new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                return UdaUtils.filterChinese(source.toString());
            }
        }});
    }
    private void completeAccount(String usrName, String passwd) {
        showProgressDialog();
        onNewRequestCall(RegisterStep3Action.newInstance(getContext(), usrName, passwd, mAuthCode, mMobile).request(new AHttpService.IResCallback<PbBean.PbResUserRegisterStep3>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserRegisterStep3> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    closeProgressDialog();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    PbBean.PbResUserRegisterStep3 body = response.body();
                    if(body != null) {
                        ConfigUtils.setCurrentUser(getContext(), new UserSession(body.getSessionId(), body.getToken()));
                        UserEvent event = new UserEvent();
                        event.setEvent(UserEvent.EVENT_STEP_3);
                        event.setExtra(body.getUserInfo().getAvatar().getUrl());
                        EventBusUtil.post(event);
                    }
                }

            }
        }));
    }

}
